package com.luban.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @author leimingchao
 * create 2025-04-19 11:25
 */
//@Component
public class LubanBeanPostProcessor implements BeanPostProcessor, MergedBeanDefinitionPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if ("userService".equals(beanName)) {

			System.out.println("初始化前:" + beanName);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {

			System.out.println("初始化后:" + beanName);
		}
		return bean;
	}

	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {

		if ("userService".equals(beanName)) {
			////这个时候，修改beanClass不起作用，因为已经实例化了
			//beanDefinition.setBeanClass(User.class);

			beanDefinition.setInitMethodName("initLuban");

		}
	}
}
